/*
*	Purpose:	To provide an easy to use super high-resolution timer.
*	
*	Usage:		Create the timer and call GetElapsed...().
*				To restart the timer, call Restart()
*
*	NOTE:		The timer starts ticking immediatly after its creation!
*/
#pragma once

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winbase.h>

class Timer
{
private:
	LARGE_INTEGER m_ticksPerSecond, m_startTicks, m_endTicks, m_cpuTime;
	bool m_running;

public:
	
	Timer()
	{
		m_running = true;
		QueryPerformanceFrequency(&m_ticksPerSecond);	// get our cpu ticks
		Restart();		// start the timer
	}

	double GetElapsedSeconds()
	{
		if(m_running)
			QueryPerformanceCounter(&m_endTicks);
		m_cpuTime.QuadPart = m_endTicks.QuadPart - m_startTicks.QuadPart;
		return ((double)m_cpuTime.QuadPart/(double)m_ticksPerSecond.QuadPart);
	}

	double GetElapsedMilliseconds()
	{
		if(m_running)
			QueryPerformanceCounter(&m_endTicks);
		m_cpuTime.QuadPart = m_endTicks.QuadPart - m_startTicks.QuadPart;
		return (((double)m_cpuTime.QuadPart/(double)m_ticksPerSecond.QuadPart) * 1000);
	}

	void Restart()	{QueryPerformanceCounter(&m_startTicks); m_running = true;}


	void Stop()
	{
		m_running = false;
		QueryPerformanceCounter(&m_endTicks);
	}
	inline void Resume()	{m_running = true;}
	inline bool IsRunning() {return m_running;}

};
